﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class BaoTapChiDAO
    {
#region 1. Inserting
        public static bool ThemBaoTapChi(BaoTapChiDTO btc)
        {
            bool result = false;
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", btc.MaBaoTC));
                sqlParams.Add(new SqlParameter("@MaNXB", btc.MaNXB ));
                sqlParams.Add(new SqlParameter("@NamPhatHanh", btc.NamPhatHanh ));
                sqlParams.Add(new SqlParameter("@DinhKy", btc.DinhKy ));
                sqlParams.Add(new SqlParameter("@KhoGiay", btc.KhoGiay ));
                sqlParams.Add(new SqlParameter("@SoTrang", btc.SoTrang ));
                sqlParams.Add(new SqlParameter("@TuaDe", btc.TuaDe ));

                // call stored procedure :))
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertBAOTAPCHI", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
#endregion
#region 2. Updating
        public static bool SuaBaoTapChi(BaoTapChiDTO btc)
        {
            bool result = false;
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", btc.MaBaoTC));
                sqlParams.Add(new SqlParameter("@MaNXB", btc.MaNXB));
                sqlParams.Add(new SqlParameter("@NamPhatHanh", btc.NamPhatHanh));
                sqlParams.Add(new SqlParameter("@DinhKy", btc.DinhKy));
                sqlParams.Add(new SqlParameter("@KhoGiay", btc.KhoGiay));
                sqlParams.Add(new SqlParameter("@SoTrang", btc.SoTrang));
                sqlParams.Add(new SqlParameter("@TuaDe", btc.TuaDe));

                // call stored procedure :))
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateBAOTAPCHI", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
#region 3. Deleting
        public static bool XoaBaoTapChi(String mabaotc)
        {
            bool result = false;
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", mabaotc));
               
                // call stored procedure :))
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteBAOTAPCHI", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
#region 4. Retrieving
        public static BaoTapChiDTO TimBaoTapChiBangID(String mabaotc)
        {
            BaoTapChiDTO result = new BaoTapChiDTO();
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", mabaotc));

                // call stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectBAOTAPCHI", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaBaoTC      = dr["MaBaoTC"].ToString();
                result.MaNXB        = dr["MaNXB"].ToString();
                result.NamPhatHanh  = SqlDataAccessHelper.ParseInt(dr["NamPhatHanh"].ToString());
                result.DinhKy       = dr["DinhKy"].ToString();
                result.KhoGiay      = dr["KhoGiay"].ToString();
                result.SoTrang = SqlDataAccessHelper.ParseInt(dr["SoTrang"].ToString());
                result.TuaDe        = dr["TuaDe"].ToString();
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaBaoTapChi()
        {
           DataTable dt= new DataTable();
            try
            {
                // call stored procedure
                dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectBAOTAPCHIsAll");
               
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static List<BaoTapChiDTO> TimBaoTapChiBangMaNXB(String manxb)
        {
            List<BaoTapChiDTO> result = new List<BaoTapChiDTO>();
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaNXB", manxb));

                // call stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectBAOTAPCHIsByAndMaNXB", sqlParams);
                foreach(DataRow dr in dt.Rows)
                {
                    BaoTapChiDTO tmp = new BaoTapChiDTO();

                    tmp.MaBaoTC     = dr["MaBaoTC"].ToString();
                    tmp.MaNXB       = dr["MaNXB"].ToString();
                    tmp.NamPhatHanh = SqlDataAccessHelper.ParseInt(dr["NamPhatHanh"].ToString());
                    tmp.DinhKy      = dr["DinhKy"].ToString();
                    tmp.KhoGiay     = dr["KhoGiay"].ToString();
                    tmp.SoTrang     = SqlDataAccessHelper.ParseInt(dr["SoTrang"].ToString());
                    tmp.TuaDe       = dr["TuaDe"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static bool CheckBaoTapChiBangID(String mabaotc)
        {
            bool result = false;
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", mabaotc));

                // call stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectBAOTAPCHI", sqlParams);
                if (dt.Rows.Count == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
    }
}
